﻿using System;
using System.Linq;

namespace Shen.NET
{
    public class Vector
    {
        public Vector(Decimal length, Context context)
        {
            _array = new Object[length.AsInt()];
            var fail = context.Intern("fail!");

            foreach (var i in 0.To(_array.Length))
                _array[i] = fail;
        }

        private readonly Object[] _array;

        public Object this[Decimal index]
        {
            get { return _array[index.AsInt()]; }
            set { _array[index.AsInt()] = value; }
        }

        public override int GetHashCode()
        {
            return _array.Where(item => item != null).Aggregate(42, (hash, item) => hash * 37 + item.GetHashCode());
        }

        public override bool Equals(object obj)
        {
            var that = obj as Vector;

            return that != null
                && _array.Length == that._array.Length
                && 0.To(_array.Length).All(i => Equals(_array[i], that._array[i]));
        }

        public override string ToString()
        {
            return "<" + String.Join(", ", _array) + ">";
        }

        public static bool operator ==(Vector a, Vector b)
        {
            return Equals(a, b);
        }

        public static bool operator !=(Vector a, Vector b)
        {
            return !Equals(a, b);
        }
    }
}
